package com.itao.minio.client;

import com.itao.minio.util.MinioUtil;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.RemoveBucketArgs;

/**
 * minio bucket操作
 * @Author sjt
 * @CreateTime 2024/5/6 14:30:27
 */
public class BucketTest {

    /**
     * 检查bucket是否存在
     */
    public static boolean bucketExists() throws Exception {
        BucketExistsArgs bucketExistsArgs = BucketExistsArgs
                .builder()
                .bucket("test-minio")
                .build();
        return MinioUtil.getMinioClient().bucketExists(bucketExistsArgs);
    }

    /**
     * 创建bucket
     */
    public static void makeBucket() throws Exception {
        boolean bucketExists = bucketExists();
        if (!bucketExists) {
            MakeBucketArgs makeBucketArgs = MakeBucketArgs
                    .builder()
                    .bucket("test-minio")
                    .build();
            MinioUtil.getMinioClient().makeBucket(makeBucketArgs);
            System.out.println("创建bucket成功");
        }
    }


    /**
     * 删除bucket
     */
    public static void removeBucket() throws Exception {
        boolean bucketExists = bucketExists();
        if (bucketExists) {
            RemoveBucketArgs makeBucketArgs = RemoveBucketArgs
                    .builder()
                    .bucket("test-minio")
                    .build();
            MinioUtil.getMinioClient().removeBucket(makeBucketArgs);
            System.out.println("删除bucket成功");
        }
    }

    public static void main(String[] args) throws Exception {
        makeBucket();
    }
}
